﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL.ComplexClasses;
using DAL;
using App_Log;
using App_Utils;

namespace BLL
{
    public class BL_Client
    {
        public static Client GetClientInfo(long client_id)
        {
            Client objClient = new Client();
            try
            {
                clsLog.logMsg("Client", "GetClientInfo", "Started");

                DAL.ithubEntities db = new ithubEntities();
                tblClient _objClient = (from q in db.tblClients where q.client_id == client_id  select q).SingleOrDefault();
                if (_objClient != null)
                {
                    objClient = (Client)App_Utils.clsUtils.ConvertToObject(_objClient, objClient);
                }
                else
                {
                    objClient = null;
                }
            }
            catch (Exception ex)
            {
                clsLog.logError("User", "GetClientInfo", ex.Message);
            }
            return objClient;
        }

        public static List<Client> GetClients(App_Utils.jQueryDataTableParamModel param, out int totalRecord)
        {
            List<Client> objClient = new List<Client>();
            totalRecord = 0;
            try
            {
                clsLog.logMsg("Client", "GetClients", "Started");

                DAL.ithubEntities db = new ithubEntities();
                var query = (from q in db.tblClients where q.is_visible == true select q);

                totalRecord = query.Count();

                // Sorting here

                query = query.OrderByDescending(m => m.create_date);

                List<tblClient> _objClient = query.Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();
                foreach (tblClient item in _objClient)
                {
                    objClient.Add((Client)App_Utils.clsUtils.ConvertToObject(item, new Client()));
                }
            }
            catch (Exception ex)
            {
                clsLog.logError("User", "GetClients", ex.Message);
            }
            return objClient;
        }

        public static Client SaveRecord(Client objClient)
        {
            try
            {
                clsLog.logMsg("Client", "SaveRecord", "Started");

                
                DAL.ithubEntities db = new ithubEntities();
                var query = (from q in db.tblClients where q.client_id == objClient.client_id select q).SingleOrDefault();

                if (query != null)
                {
                    query = (tblClient)clsUtils.ConvertToObject(objClient, new tblClient());
                    db.SaveChanges();
                }
                else
                {
                    tblClient _objtblClient = new tblClient();
                    _objtblClient = (tblClient)clsUtils.ConvertToObject(objClient, new tblClient());
                    db.tblClients.Add(_objtblClient);
                    db.SaveChanges();
                    objClient = (Client)clsUtils.ConvertToObject(_objtblClient, objClient);
                }
            }
            catch (Exception ex)
            {
                clsLog.logError("User", "SaveRecord", ex.Message);
            }
            return objClient;
        }

    }
}
